<?php

namespace api\modules\v1\controllers;
use api\func\func;
use api\modules\v1\models\Reward;
use api\modules\v1\models\Withdraw;
use api\modules\v1\models\User;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
use Yii;
use yii\web\NotFoundHttpException;
use api\modules\v1\controllers\PayController;
class RewardController extends PayController
{
    public $modelClass = 'api\modules\v1\models\Reward';

    public function beforeAction($action)
    {
        $this->enableCsrfValidation = true;
        return parent::beforeAction($action);
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }

    public function actionCreate(){
        $model = new $this->modelClass();
        $post = Yii::$app->request->post();
        $model->attributes = $post;
        if($model->save()){
            $return_data = '';
            if($post['pay_type'] == 1){ //调用支付宝 移动app使用的SDK数据
                $return_data = $this->alipaySdk($model->getAttributes());
            }elseif($post['pay_type'] == 2){ //调用微信 移动app使用的SDK数据
                $return_data = $this->weixinSdk($model->getAttributes());
            }
            return [
                "sdk"=>$return_data,
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 打赏总览
     */
    public function actionPandect(){
        $uid = Yii::$app->user->identity->id;
        $balance = User::findOne(['id'=>$uid],'balance');
        $balance = $balance['balance'];//账户余额
        //统计打赏信息
        $Reward = (new \yii\db\Query())
            ->select(['count(*) as count', 'sum(money) as money'])
            ->from('{{%reward_app}}')
            ->where(['rereward_id'=>$uid,'status'=>1])
            ->groupBy('rereward_id')
            ->all();
        $rereward_data = Reward::find()->select('time,money,pay_type,reward_id,rereward_id')->where(['rereward_id'=>$uid,'status'=>1])->asArray()->orderBy('time desc')->one();//最后一次被打赏记录
        if($rereward_data){
            $reware_nickname = User::findOne(['id'=>$rereward_data['reward_id']]);
            $reware_nickname = $reware_nickname['nickname'] != '' ? $reware_nickname['nickname'] :func::replace_phone($reware_nickname['phone']);
            $rereward_data['reward_nickname'] = $reware_nickname;

            unset($rereward_data['reward_id']);
            unset($rereward_data['pay_type']);
            if(isset($rereward_data['money'])){
                $rereward_data['money'] = number_format($rereward_data['money'],2);
            }
        }

        $reward_data = Reward::find()->select('time,money,pay_type,reward_id,rereward_id')->where(['reward_id'=>$uid,'status'=>1])->asArray()->orderBy('time desc')->one();//最后一次打赏别人记录
        if($reward_data){
            $reware_nickname = User::findOne(['id'=>$reward_data['rereward_id']]);
            $reware_nickname = $reware_nickname['nickname'] != '' ? $reware_nickname['nickname'] :func::replace_phone($reware_nickname['phone']);
            $reward_data['reward_nickname'] = $reware_nickname;

            unset($reward_data['reward_id']);
            unset($reward_data['pay_type']);
            if(isset($reward_data['money'])){
                $reward_data['money'] = number_format($reward_data['money'],2);
            }
        }

        $withdraw_data = Withdraw::find()->select('time,money,withdraw_type,account')->where(['userid'=>$uid,'status'=>3])->asArray()->orderBy('id desc')->one();//最后一次提现记录
        if(isset($withdraw_data['money'])) {
            $withdraw_data['money'] = number_format($withdraw_data['money'],2);
        }
        $data = [];
        $data['balance'] = number_format($balance,2);
        $data['count'] = isset($Reward[0]['count']) ? $Reward[0]['count'] :0;
        $data['money'] = isset($Reward[0]['money']) ? number_format($Reward[0]['money'],2) : 0.00;
        $data['reward'] = is_array($reward_data) ? $reward_data : (object)[];
        $data['rereward'] = is_array($rereward_data) ? $rereward_data : (object)[];
        $data['withdraw'] = is_array($withdraw_data) ? $withdraw_data : (object)[];
        return [
            "pandect"=>$data,
        ];
    }

    /*
     * 打赏记录
     */
    public function actionList(){
        $model = new $this->modelClass();
        return $model->getList();
    }
}
